package com.itcam.list_.collection_.list_;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Author:Cammy.Wu
 * Date:2024-06-02
 * Description:
 */

public class ListDemo04 {
    /**
     * 迭代器遍历集合的过程中，使用集合的删除方法，删除倒数第二个元素，就不会出现错误
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        list.add("abc");
        list.add("def");
        list.add("ghi");
        list.add("jkl");
        list.add("nm");

        Iterator<String> it = list.iterator();

        while (it.hasNext()) {
            String s = it.next();
            if ("jkl".equals(s)) {
                list.remove("jkl");
            }
        }
        System.out.println("list = " + list);
    }
}
